#include <windows.h>
#include <stdio.h>
#include "../../../lib/vmpsdk/1.6/VMProtectSDK.h"

#include "../../../lib/GetHDSN/GETHDSN.h"
#pragma comment(lib,"./lib/GetHDSN/Release/Win32GetHDSN.lib")

#include "../../../lib/Crypt/crypt2.h"
#pragma comment(lib,"./lib/Crypt/Cryptst.lib")

#include "../../../lib/KeyProceptLib12/LisKeyProtect.h"
#pragma comment(lib,"./lib/KeyProceptLib12/KeyProceptLibV12.lib")

#include "../MsgFormatTool/msgformatout.h"
#include "../MsgFormatTool/MsgFormatTool.h"

//#pragma comment(lib,"./lib/MsgFormatTool/MsgFormatTool.lib")

#pragma comment(lib,"Netapi32.lib")

//out
#include "softlic_core.h"



DWORD WINAPI GetCurNTVolum(CHAR OUT *szretVolum,UINT nSize)
{
	TCHAR szCurNTPath[MAX_PATH] = { 0 };

	TCHAR szCurRoot[4] = { 0 } ;

	TCHAR szVolum[MAX_PATH] = { 0 } ;

	TCHAR szFileSysInfo[32] = { 0 };

	DWORD dwVolumSize,MaximumComponentLength,FileSystemFlags  = 0 ;
	

	GetWindowsDirectory(szCurNTPath,sizeof(szCurNTPath)/sizeof(TCHAR));

	memcpy(szCurRoot,szCurNTPath,3 * sizeof(TCHAR) ) ;

	szCurRoot[3] = 0;

	GetVolumeInformation(szCurRoot,szVolum,sizeof(szVolum),&dwVolumSize,&MaximumComponentLength,&FileSystemFlags,szFileSysInfo,sizeof(szFileSysInfo) );

	strncpy(szretVolum,szVolum,min(strlen(szVolum),nSize));

	return strlen(szVolum);
}

static	BYTE	g_this_machine_hardware_fingerprint[16] = { 0 } ;

UINT WINAPI		GetHardWareFinger(CHAR OUT *szFingerMSG,UINT nSize)
{
	
	DWORD	dwRet = 0 ;

	DWORD	dwSize = 0 ;

	BYTE	bHardSN[20+1] = { 0 } ;

	BYTE	bMD5Buffer[16] = { 0 };

	

	VMProtectBegin

	dwRet =  GetHardDiskSN(bHardSN,sizeof(bHardSN));

	

	if ( 0 == dwRet )
	{
		
		dwRet = GetCurNTVolum(bHardSN,sizeof(bHardSN));
		
	}

	if (dwRet)
	{
		Digest(DA_MD5,bHardSN,dwRet,bMD5Buffer,&dwSize);
	}
	else
	{
		strncpy(bMD5Buffer,"HDD_NOT_SUPPORT",16);
	}
		
	
	ByteSteamFormatToSerialNumberA_code(bMD5Buffer,sizeof(bMD5Buffer),szFingerMSG,nSize,&dwSize);

	memcpy(g_this_machine_hardware_fingerprint,bMD5Buffer,sizeof(bMD5Buffer));

	VMProtectEnd

	return NO_ERROR;
}



UINT	WINAPI	f_IsLicense(LPCSTR szSOFT_MOD_ID,VOID *bkeybuf,UINT nkeybufsize,OUT VOID *bkeyContext,IN UINT sizet,OUT UINT *lpretSize,OUT SYSTEMTIME *lpsystime)
{
	
		SYSTEMTIME	st_sysTime = { 0 };

		BYTE	Context_IN_licensekey[128] = { 0 }; 

		DWORD	dwRet,dwRetSize = 0 ;
		
		VMProtectBegin
		
		dwRet = IsLienseRightExA_Ver12(g_this_machine_hardware_fingerprint,
								sizeof(g_this_machine_hardware_fingerprint),
								szSOFT_MOD_ID,
								bkeybuf,nkeybufsize,
								&st_sysTime,
								&Context_IN_licensekey[0],sizeof(Context_IN_licensekey),
								&dwRetSize
								);


		if (dwRet != RSA_RET_SUCCESSFULLY)

		{

			return	dwRet;
		}

		if (lpretSize)
		{
			*lpretSize = dwRetSize;
		}

		if (bkeyContext)
		{
			memcpy(bkeyContext,&Context_IN_licensekey[0],min(sizet,dwRetSize));
		}

		if (lpsystime)
		{
			memcpy(lpsystime,&st_sysTime,sizeof(st_sysTime));
		}


		return	NO_ERROR ;

		VMProtectEnd
}